package com.freewater.parking.infrastructure.repository;

import com.freewater.parking.infrastructure.db.ParkingDao;
import com.freewater.parking.infrastructure.db.ParkingTable;
import com.freewater.parking.domain.parking.Parking;
import com.freewater.parking.domain.parking.ParkingRepository;
import com.freewater.parking.domain.common.PlateNumber;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public class ParkingRepositoryImpl implements ParkingRepository {

    private final ParkingDao parkingDao;

    public ParkingRepositoryImpl(ParkingDao parkingDao) {
        this.parkingDao = parkingDao;
    }

    @Override
    public void save(Parking parking) {
        this.parkingDao.save(new ParkingTable(parking));
    }

    @Override
    public Optional<Parking> findByPlateNumber(PlateNumber plateNumber) {
        return this.parkingDao.findById(plateNumber.getValue())
                .map(ParkingTable::toParking);
    }

    @Override
    public void delete(Parking parking) {
        this.parkingDao.deleteById(parking.getPlateNumber().getValue());
    }
}
